﻿@model YiYiTong.Rocket.Model.Manager.RoleModel

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<form action="@Url.Action("AddRole","Role")" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td colspan="2">
                <h5>
                    @ViewBag.Title，请填写角色信息，标<em>*</em>号是必填或必选项。
                </h5>
                <input type="hidden" value="@Model.roleId" name="RoleID" />
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">角色代码<em>*</em></td>
            <td><input type="text" id="RoleCode" name="RoleCode" maxlength="50" value="@Model.roleCode" /></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">角色名称<em>*</em></td>
            <td><input type="text" name="RoleName" maxlength="50" value="@Model.roleName" /></td>
        </tr>
        <tr>
            <td class="tableleft">备注</td>
            <td><textarea name="Remark" style="width:240px;" maxlength="250" cols="4">@Model.remark</textarea></td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <p><span id="spMsg"></span></p>
                <button type="submit" class="btn btn-primary">保存</button> &nbsp;&nbsp;
                <button type="button" class="btn btn-success" name="backid" id="backid">返回</button>
            </td>
        </tr>
    </table>
    <script>
        BUI.use('bui/overlay', function (overlay) {
            function showSucc(obj, url) {
                BUI.Message.Alert(obj, function () {
                    location.href = url;
                }, 'success');
            }
            $(function () {
                $('#backid').on('click', function () {
                    window.document.location.href = "@Url.Action("RoleList", "Role")";
                });

                //判断中文字符
                jQuery.validator.addMethod("isChinese", function (value, element) {
                    return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);
                }, "只能包含中文字符");

                //判断只能字母
                jQuery.validator.addMethod("IsLetter", function (value, element) {
                    return this.optional(element) || /^[a-zA-Z]+$/.test(value);
                }, "只能包含字母");

                var validator = $('form').validate({
                    errorLabelContainer: $("#errorContainer"),
                    errorElement: "span",
                    rules: {
                        RoleCode: { required: true, IsLetter: true },
                        RoleName: { required: true, isChinese: true }
                    },
                    messages: {
                        RoleCode: { required: "角色代码不能为空", IsLetter: "角色代码只能包含字母" },
                        RoleName: { required: "角色名称不能为空", isChinese: "角色名称只能包含中文字符" }
                    },
                    submitHandler: function (form) {
                        $(form).ajaxSubmit(function (result) {
                            if (result.Message != undefined) {
                                if (result.IsSuccess) {
                                    showSucc(result.Message, result.Body);
                                }
                                else {
                                    $("#spMsg").html(result.Message);
                                }
                                return false;
                            }
                            else {
                                var substr = cutstr(result, "BUI.Message.Alert", "function");
                                substr = substr.replace("'", "").replace("'", "").replace("(", "").replace(",", "");
                                if (substr == "")
                                    BUI.Message.Alert("操作失败，请联系技术人员", 'warning');
                                else {
                                    if (substr.indexOf("请重新登录") > 0) {
                                        BUI.Message.Alert(substr, function () {
                                            top.location.href = "@Url.Action("Login","User")";
                                        }, 'warning');
                                    }
                                    else {
                                        BUI.Message.Alert(substr, 'warning');
                                    }
                                }
                            }
                        });
                    }
                });
            });
        });
    </script>
</form>
